//
//  CousesViewController.m
//  TraineeManagementSystem
//
//  Created by Dhanashree Bodas on 23/01/15.
//  Copyright (c) 2015 Dhanashree Bodas. All rights reserved.
//

#import "CoursesViewController.h"
#import "CourseTable.h"
#import "Course.h"


@interface CoursesViewController ()

@end

@implementation CoursesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self loadData];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)loadData{
    
    CourseTable *c=[[CourseTable alloc]init];
    self.cdata=[c listCourse];
    [self.courseTableview reloadData];
    
}

-(void) viewWillAppear:(BOOL)animated{
    
    [self loadData];
    [self.courseTableview reloadData];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)addCourse:(id)sender {
}

#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _cdata.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    Course *course=[self.cdata objectAtIndex:[indexPath row]];
    cell.textLabel.text=course.cname;
    cell.detailTextLabel.text=course.platform;
    return cell;

    
}


-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the selected record.
        // Find the record ID.
        Course *c1=[self.cdata objectAtIndex:[indexPath row]];
        int recordIDToDelete = c1.cid;
        
        CourseTable *c=[[CourseTable alloc] init];
        
        int cnt=[c deleteCourse:recordIDToDelete];
        
        NSLog(@"cnt=%d",cnt);
        if(cnt>0)
        {
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Delete Course" message:@"Course Deleted successfully!!!" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
            [alert show];
            
        }
        
        [self loadData];
    }
}

@end
